package com.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class LoginController implements Controller{

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String username = request.getParameter("username");
		String pwd = request.getParameter("password");
		
		String message = null;
		Map<String,String> model= new HashMap<String,String>();
		if(username == null || pwd ==null || username.trim().equals("") ||
				pwd.trim().equals("")){
			message="username or password is null or empty";
			model.put("msg", message);
			return new ModelAndView(getErrorPage(),model);
		}
		
		if(username.equals("admin") && pwd.equals("admin")){
			message="login success";
			model.put("msg", message);
			model.put("username", username);
			return new ModelAndView(getSuccessPage(),model);
		}
		
		message="username or password is wrong!";
		model.put("msg", message);
		return new ModelAndView(getErrorPage(),model);
	}

	private String successPage;
	public String getSuccessPage() {
		return successPage;
	}
	public void setSuccessPage(String successPage) {
		this.successPage = successPage;
	}
	public String getErrorPage() {
		return errorPage;
	}
	public void setErrorPage(String errorPage) {
		this.errorPage = errorPage;
	}

	private String errorPage;
}
